<?php

namespace catchAdmin\model;

use app\Request;
use catcher\base\CatchModel;
use catcher\exceptions\AlertErrorException;
use think\facade\Db;

class ModelApp extends CatchModel
{


    protected $hidden = [
        'creator_id',
        'deleted_at',
        'updated_at',
    ];


    public function append(array $append = [])
    {
        return parent::append(array_merge($append, $this->append));
    }


    /**
     * 保存
     */
    public function saveDataCC($param)
    {
        foreach ($param as $key => $value) {
            if (isset($this->type[$key]) && $this->type[$key] == 'array') {
                $param[$key] = json_encode($param[$key], JSON_UNESCAPED_UNICODE);
            }
        }

        if (empty($param['id'])) {
            $param['created_at'] = time();
            $param['creator_id'] = $this->getLoginId();

            $field_list = [];
            foreach ($this->getFields() as $field) {
                $field_list[$field['name']] = $field['name'];
            }
            foreach ($param as $k => $v) {
                if (!isset($field_list[$k])) {
                    unset($param[$k]);
                }
            }

            $id = $this->insertGetId($param);
        } else {
            $id = $param['id'];
            $param['updated_at'] = time();
            unset($param['id']);
            unset($param['created_at']);

            if (empty($this
                ->where('id', $id)
                ->find())) {
                throw new AlertErrorException('数据不存在');
            }

            $field_list = [];
            foreach ($this->getFields() as $field) {
                $field_list[$field['name']] = $field['name'];
            }
            foreach ($param as $k => $v) {
                if (!isset($field_list[$k])) {
                    unset($param[$k]);
                }
            }

            $this
                ->where('id', $id)
                ->update($param);
        }

//        echo Db::getLastSql();

        return $this
            ->where('id', $id)
            ->find();
    }


    public function indexDataCC($param, $this_app = null, $append = [])
    {
        if (isset($param['page_no']) && !is_numeric($param['page_no'])) {
            $param['page_no'] = 1;
        }
        if (empty($param['page_no']) || $param['page_no'] < 1) {
            $param['page_no'] = 1;
        }

        if (isset($param['page_size']) && !is_numeric($param['page_size'])) {
            $param['page_size'] = 10;
        }
        if (empty($param['page_size'])) {
            $param['page_size'] = 10;
        }

        if (empty($this_app)) {
            $app = $this;
        } else {
            $app = $this_app;
        }

        $list = $app
            ->catchSearch()
            ->catchOrder()
            ->limit(($param['page_no'] - 1) * $param['page_size'], $param['page_size'])
            ->select()
            ->each(function ($item) use ($append) {
                $item->append(array_merge($item->append, $append));
            });

        $count = $app->count();

//        echo Db::getLastSql();

        return [
            'count'      => $count,
            'page_no'    => $param['page_no'],
            'page_size'  => $param['page_size'],
            'page_count' => ceil($count / $param['page_size']),
            'list'       => $list,
        ];
    }


    public function indexDataPcCC($param, $this_app = null, $append = [])
    {
        if (isset($param['page']) && !is_numeric($param['page'])) {
            $param['page'] = 1;
        }
        if (empty($param['page']) || $param['page'] < 1) {
            $param['page'] = 1;
        }

        if (isset($param['limit']) && !is_numeric($param['limit'])) {
            $param['limit'] = 20;
        }
        if (empty($param['limit'])) {
            $param['limit'] = 20;
        }

        if (empty($this_app)) {
            $app = $this;
        } else {
            $app = $this_app;
        }

        if ($param['limit'] == 999) {
            $param['page'] = 1;
        }

        $list = $app
            ->catchSearch()
            ->catchOrder()
            ->limit(($param['page'] - 1) * $param['limit'], $param['limit'])
            ->select()
            ->each(function ($item) use ($append) {
                $item->append(array_merge($item->append, $append));
            });

        $count = 0;
        if ($param['limit'] != 999) {
            $count = $app->count();
        }

//        echo Db::getLastSql();

        return [
            'count'   => $count,
            'current' => $param['page'],
            'limit'   => $param['limit'],
            'data'    => $list,
        ];
    }


    public function getPcLoginId()
    {
        return (new Request())->user()['id'];
    }


    public function getPcLoginInfo()
    {
        return (new Request())->user();
    }


    public function getPcLoginWechatUserId()
    {
        return (new Request())->user()['wechat_user_id'] ?? 0;
    }


    /**
     * 登录人——id
     */
    public function getLoginId()
    {
        return session('login_id');
    }


    /**
     * 登录人——id
     */
    public function getLoginUser()
    {
        return (new \catchAdmin\model\WechatUser())
            ->where('id', $this->getLoginId())
            ->find();
    }


    /**
     * 是否是公司
     */
    public function getCompanyIs()
    {
        return $this->getLoginUser()['company_status'] == 3;

        return $this->getLoginUser()['company_type'] > 1 && $this->getLoginUser()['company_status'] == 3;
    }

}
